<?php
class Discussion_model extends CI_Model{
    //Ham dung va truy cap co so du lieu
    public function __construct(){
        parent::__construct();
        $this->load->database();
    }
    
    //Ham load du lieu tu bang question_disc_nmh
    public function listall_ques(){
        $query=$this->db->get("question_disc_nmh");
        return $query->result_array();
    }  
    
        //Ham load du lieu tu bang question_disc_nmh
    public function listall_ques_by_id($course_id){
        $sql = "SELECT *
                FROM question_disc_nmh
                WHERE course_id = $course_id
                ORDER BY disc_ques_id DESC";
        $query=$this->db->query($sql);
        return $query->result_array();
    } 
    
    //Ham load noi dung cau tra loi
    public function ans_get_content($id,$column) {
        $this->load->database();
        switch ($column) {
            case "content":
                $this->db->select("disc_ans_content");
                break;
            case "id":
                $this->db->select("disc_ans_id");
                break;
            case "email":
                $this->db->select("disc_ans_email");
                break;
            case "time":
                $this->db->select("disc_ans_time");
                break;
            case "name":
                $this->db->select("disc_ans_name");
                break;
        }
        
        $this->db->where("disc_ques_id","$id");
        $query = $this->db->get("answer_disc_nmh");
        //$query = $this->db->query("SELECT disc_ans_id, disc_ans_email, disc_ans_content, disc_ans_time 
        //FROM answer_disc_nmh WHERE disc_ques_id=$id;");
        $content = array();
        $i = 0;
        foreach ($query->result() as $content_1) {
            switch ($column) {
                case "content":
                    $str = strip_tags($content_1->disc_ans_content);
                    break;
                case "id":
                    $str = strip_tags($content_1->disc_ans_id);
                    break;
                case "email":
                    $str = strip_tags($content_1->disc_ans_email);
                    break;
                case "time":
                    $str = strip_tags($content_1->disc_ans_time);
                    break;
                case "name":
                    $str = strip_tags($content_1->disc_ans_name);
                    break;
            }
            $content[$i] = $str;
            $i = $i + 1;
        }
        //echo "Kiem tra mang $content trong Model";
        //var_dump($content);        
        return $content;
    }
    
    //Ham load du lieu tu bang question_dis_nmh voi disc_ques_id = id
    public function ques_get($id) {
        $this->load->database();
        $query = $this->db->query("SELECT disc_ques_id,
                                          disc_ques_name,
                                          disc_ques_email,
                                          disc_ques_title,
                                          disc_ques_content,
                                          disc_ques_time, 
                                          disc_ques_quantity_ans 
                                    FROM question_disc_nmh 
                                    WHERE disc_ques_id=$id;");
        return $query->row_array();
    }
    
    //Them ban ghi cau tra loi vao CSDL
    public function insert_ans($data_ans) {
        $ques_quan = $data_ans['ques_quantity_ans'] + 1;
        //$f1 = $data_Answer['ques_quantity_ans'];
        $id_ques = $data_ans['id_ques'];        
        $txt_ans = $data_ans['txt_ans'];
        $time = $data_ans['ans_time'];
        $lecture_id = "1";
        $course_id = $data_ans['course_id'];
        $email = $data_ans['user']['EMAIL'];
        $username = $data_ans['user']['USERNAME'];
        $this->db->query("INSERT INTO answer_disc_nmh(disc_ques_id, 
                                                      disc_ans_email,
                                                      disc_ans_content,
                                                      disc_ans_time,
                                                      lecture_id,
                                                      course_id,
                                                      disc_ans_name) 
                                               VALUES(
                                                      '$id_ques',
                                                      '$email',
                                                      '$txt_ans',
                                                      '$time',
                                                      '$lecture_id',
                                                      '$course_id',
                                                      '$username')");
                                                      
        $sql_update = "UPDATE question_disc_nmh
                        SET disc_ques_quantity_ans = $ques_quan
                        WHERE disc_ques_id = $id_ques";
        $this->db->query($sql_update);
        
        $ans_quan = $data_ans['user']['ans_quan'] + 1;
        $user_id = $data_ans['user']['ID'];
        $this->db->query("UPDATE user
                    SET ANS_QUAN = '$ans_quan'
                    WHERE ID = '$user_id'");

        

    }
    
    //Them ban ghi cau hoi vao CSDL
    public function insert_ques($data_ques){
        
        $name = $data_ques['name'];
        $email = $data_ques['email'];
        $title_of_ques = $data_ques['title_of_ques'];
        $txt_ques = $data_ques['txt_ques'];
        $time = $data_ques['ques_time'];
        $lecture_id = '1';
        $disc_ques_quantity_ans = '0';
        $course_id  = $data_ques['course_id'];
        
        $this->load->database();
        $this->db->query("INSERT INTO question_disc_nmh(disc_ques_name,
                                                        disc_ques_email,
                                                        disc_ques_title,
                                                        disc_ques_content,
                                                        disc_ques_time,
                                                        lecture_id,
                                                        course_id,
                                                        disc_ques_quantity_ans)
                                                 VALUES('$name',
                                                        '$email',
                                                        '$title_of_ques',
                                                        '$txt_ques',
                                                        '$time',
                                                        '$lecture_id',
                                                        '$course_id',
                                                        '$disc_ques_quantity_ans')");
        $ques_quan = $data_ques['ques_quan'];
        $ID = $data_ques['ID'];
        $this->db->query("UPDATE user
                            SET QUES_QUAN = '$ques_quan'
                            WHERE ID = '$ID'");
        
    }
     
    
}
    
?>



